
####################################################################################################################

#Table A 1
#This table compares the 10 districts studied with the remaining 14 districts
####################################################################################################################


#######################
# Set color schemes 

cColor <- "#97B8DE"
tColor <- "#4669BD"
eColor <- "#B1ACB7"

orig.dat <- read.csv(paste(AdminDataDir, "District_comparison.csv", sep =""), stringsAsFactors = FALSE)


############## Balance table #################

# balance vars
all.bal.vars <- c("total.pop", "pop.increase.01.11", "pop.density","literacy", 
                  "avg.fps.size", "Beneficiaries_FPS", "avg.rural.benef","avg.aay.benef","num.blocks",
                  "salary_income","mpce_mrp", "tot_cons_value_food","tot_cons_value_fuel_light")

all.bal.vars.names <- c("Population in 2011","Population growth, 2001-2011", "Population density","% Literate", 
                        "Ration cards per FPS", "Beneficiaries per FPS", "% FPS rural", "% AAY beneficiares","Number of blocks",
                        "% With salary income", "Monthly per capita consumption", "Consumption value food", "Consumption value fuel/light" )

stopifnot(length(all.bal.vars.names) == length(all.bal.vars))

#creates matrix for table 
summ <- matrix(NA, ncol = 4, nrow = length(all.bal.vars))
rownames(summ) <- all.bal.vars.names
colnames(summ) <- c("Study district", "Non-study district", "Difference", "p-value")

# ttest 
for (s in seq_along(all.bal.vars)) {
  
  this.var <- all.bal.vars[s]
  
  if (max(orig.dat[[this.var]]) <= 1) di <- 2
  else if (max(orig.dat[[this.var]]) > 1 & min(orig.dat[[this.var]]) < 10) di <- 1
  else di <- 0
  
  temp <- t.test(orig.dat[[this.var]] ~ orig.dat[["study.dist"]])
  
  summ[s,1] <- round(temp$estimate[2], di)
  summ[s,2] <- round(temp$estimate[1], di)
  summ[s,3] <- round(temp$estimate[2] - temp$estimate[1], di)
  summ[s,4] <- round(temp$p.value,3)
  
}  


tmp.N <- t(c(sum(orig.dat$study.dist == 1), sum(orig.dat$study.dist == 0), NA, NA))
colnames(tmp.N) <- colnames(summ)
rownames(tmp.N)[1] <- "N"
summ <- rbind(summ,tmp.N)


#create latex table
out.main <- stargazer::stargazer(summ, type = "latex", p = summ[,4],digits = 2,header=FALSE,float=FALSE, model.numbers=TRUE,digit.separator=",")

print(out.main)

#formatting latex table 

#1) add row

star.out <- starpolishr::star_insert_row(out.main, c("\\emph{Panel A: 2011 Census}  &  & &  &  \\\\ ","\\emph{Panel B: Beneficiary List}  &  &  &  &  \\\\ ","\\emph{Panel C: NSS 68}  &  &  &  &  \\\\ "), insert.after = c(6, 10,15))
star.out <- starpolishr::star_insert_row(star.out, c("\\cmidrule(lr){2-2} \\cmidrule(lr){3-3} \\cmidrule(lr){4-4} \\cmidrule(lr){5-5}  &\\multicolumn{1}{c}{(1)}&\\multicolumn{1}{c}{(2)}&\\multicolumn{1}{c}{(3)}&\\multicolumn{1}{c}{(4)}\\\\"), insert.after = c(5))




#2) add lines
star.out2 <- starpolishr::star_insert_row(star.out, c("\\hline \\\\[-1.8ex]","\\hline \\\\[-1.8ex]","\\hline \\\\[-1.8ex]" ), insert.after = c(12, 18,23))

#3) left align 
star.out2 <- sub(" \\\\centering", "", star.out2)
star.out2 <- sub("ccccc", "lrrrr", star.out2)
star.out2 <- sub("$", " ", star.out2)
star.out2 <- sub("p-value" , "$p$-value", star.out2)




# lastly output to tex file 
star_tex_write(star.out2, file = paste(OutputDir,"TableA_1.tex",sep=""))



